package com.zqs.mapper;

import com.zqs.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;

@Repository
public interface UserMapper {

    /**
     * 注册
     * @param user 用户对象
     * @return 返回影响的行数
     */
    @Insert("insert into t_user (username,password,salt,phone,email,avatar,gender,is_delete,created_user," +
            "created_time,modified_user,modified_time) values(#{username},#{password},#{salt},#{phone},#{email}," +
            "#{avatar},#{gender},#{isDelete},#{createdUser},#{createdTime},#{modifiedUser},#{modifiedTime})")
    Integer reg(User user);

    /**
     * 根据用户名查用户
     * @param username 用户输入的名称
     * @return 返回用户对象
     */
    @Select("select * from t_user where username = #{username}")
    User findByUsername(String username);

    /**
     * 更新用户的信息
     * @param phone 用户输入的电话号码
     * @param email 用户输入的电子邮箱
     * @param gender 用户输入的性别
     * @param uid 修改用户的id
     * @return 返回影响的行数
     */
    @Update("update t_user set avatar=#{avatar},phone=#{phone},email=#{email},gender=#{gender},modified_user=#{modifiedUser}," +
            "modified_time=#{modifiedTime} where uid=#{uid}")
    Integer updateUserInfoByUid(@Param("avatar") String avatar,
                                @Param("phone") String phone,
                                @Param("email") String email,
                                @Param("gender") Integer gender,
                                @Param("uid") Integer uid,
                                @Param("modifiedUser") String modifiedUser,
                                @Param("modifiedTime") Date modifiedTime);

//    /**
//     * 根据uid修改用户头像
//     * @param avatar 用户头像地址
//     * @param modifiedUser 修改人
//     * @param modifiedTime 修改时间
//     * @param uid 用户id
//     * @return 返回影响行数
//     */
//    @Update("update t_user set avatar=#{avatar},modified_user=#{modifiedUser},modified_time=#{modifiedTime} " +
//            "where uid=#{uid}")
//    Integer updateAvatar(@Param("avatar") String avatar,
//                         @Param("modifiedUser") String modifiedUser,
//                         @Param("modifiedTime") Date modifiedTime,
//                         @Param("uid") Integer uid);

    /**
     * 修改密码
     * @param password
     * @param modifiedUser
     * @param modifiedTime
     * @param uid
     * @return
     */
    @Update("update t_user set password=#{password},modified_user=#{modifiedUser},modified_time=#{modifiedTime} " +
            "where uid = #{uid}")
    Integer updatePasswordByUid(@Param("password") String password,
                           @Param("modifiedUser") String modifiedUser,
                           @Param("modifiedTime") Date modifiedTime,
                           @Param("uid") Integer uid);

    /**
     * 根据uid查询用户
     * @param uid
     * @return
     */
    @Select("select * from t_user where uid = #{uid}")
    User findByUid(Integer uid);
}
